package library.sdk.demo.parent;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import cn.pedant.SweetAlert.SweetAlertDialog;
import library.http.Http;
import library.permission.PermissionsManager;
import library.sdk.demo.R;
import widget.bar.SystemBar;
import widget.drag.DragLayout;
import widget.utils.ViewUtils;

/**
 * Created by Mr.Fan on 2016-4-6.
 */
public abstract class ParentActivity extends AppCompatActivity {

    private static final int BAR_ALPHA = 0xFF / 1;


    protected abstract Object inflateView();

    protected abstract void initView(View anchor);

    protected boolean enableNavigationBar() {
        return false;
    }

    protected boolean isShowStatusBarShadow() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addActivity();

        Object obj = inflateView();
        if (obj instanceof Integer)
            setContentView(Integer.parseInt(obj.toString()));
        else if (obj instanceof View)
            setContentView((View) obj);

        ViewUtils.inject(this, getWindow().getDecorView());

        initView(getWindow().getDecorView());
    }

    @Override
    public void setSupportActionBar(@Nullable Toolbar toolbar) {
        ViewGroup parent = (ViewGroup) toolbar.getParent();
        if (parent instanceof AppBarLayout) {

            ViewGroup container = (ViewGroup) parent.getParent();
            if (container != null && (container instanceof FrameLayout
                    || container instanceof RelativeLayout
                    || container instanceof DragLayout)) {
                container.removeView(parent);
                container.addView(parent);
            }


        }
        super.setSupportActionBar(toolbar);
    }


    @Override
    public void finish() {
        Http.cancel(this);
        super.finish();
    }

    @Override
    protected void onDestroy() {
        Http.cancel(this);
        super.onDestroy();
        removeActivity();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
    }

    public ParentActivity getActivity() {
        return this;
    }

    protected final void setSystemBar() {
        setSystemBarColorByResource(R.color.colorPrimary);
    }

    public final void setSystemBarTransparent() {
        if (enableNavigationBar()) {
            SystemBar.obtain()
                    .statusBar().transparent().shadow(isShowStatusBarShadow())
                    .navigationBar().transparent()
                    .apply(this);
        } else {
            SystemBar.obtain()
                    .statusBar().transparent().shadow(isShowStatusBarShadow())
                    .apply(this);
        }
    }

    public final void setSystemBarTranslucent() {
        if (enableNavigationBar()) {
            SystemBar.obtain()
                    .statusBar().translucent().shadow(isShowStatusBarShadow())
                    .navigationBar()
                    .apply(this);
        } else {
            SystemBar.obtain()
                    .statusBar().translucent().shadow(isShowStatusBarShadow())
                    .apply(this);
        }
    }

    public final void setSystemBarColorByResource(int colorId) {
        setSystemBarColor(getResources().getColor(colorId));
    }

    public final void setSystemBarColor(int color) {
        if (enableNavigationBar()) {
            SystemBar.obtain()
                    .statusBar().color(color).alpha(BAR_ALPHA).shadow(isShowStatusBarShadow())
                    .navigationBar()
                    .apply(getWindow());
        } else {
            SystemBar.obtain()
                    .statusBar().color(color).alpha(BAR_ALPHA).shadow(isShowStatusBarShadow())
                    .apply(getWindow());
        }
    }


    public void setSystemBarColorForMultiLayout(View main, int color) {
        if (enableNavigationBar()) {
            SystemBar.obtain()
                    .setContentViewForMultiLayout(main)
                    .statusBar().color(color).alpha(BAR_ALPHA).shadow(isShowStatusBarShadow())
                    .navigationBar()
                    .apply(getWindow());
        } else {
            SystemBar.obtain()
                    .setContentViewForMultiLayout(main)
                    .statusBar().color(color).alpha(BAR_ALPHA).shadow(isShowStatusBarShadow())
                    .apply(getWindow());
        }
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


    private static final Map<String, WeakReference<ParentActivity>> list =
            Collections.synchronizedMap(new LinkedHashMap<String, WeakReference<ParentActivity>>());

    public void addActivity() {
        synchronized (list) {
            list.put(String.valueOf(this), new WeakReference<>(this));
        }
    }

    public void removeActivity() {
        synchronized (list) {
            list.remove(String.valueOf(this));
        }
    }

    public static void finishAllActivities() {
        synchronized (list) {
            Iterator it = list.keySet().iterator();
            while (it.hasNext()) {
                Object key = it.next();
                WeakReference<ParentActivity> ref = list.get(key);
                if (ref != null) {
                    ParentActivity act = ref.get();
                    if (act != null) {
                        act.finish();
                    }
                }
            }
        }
    }

    public void finishOtherActivities() {
        synchronized (list) {
            Iterator it = list.keySet().iterator();
            while (it.hasNext()) {
                Object key = it.next();
                WeakReference<ParentActivity> ref = list.get(key);
                if (ref != null) {
                    ParentActivity act = ref.get();
                    if (act != null && !act.equals(this)) {
                        act.finish();
                    }
                }
            }
        }
    }


}
